<template>
  <div class="login">
    <div class="box">
      <!-- 传一个引用 -->
      <i class="el-icon-close" @click="close_logins"></i>
      <div class="content">
        <div class="nav">
          <span>密码登录</span>
        </div>
        <!-- @blur鼠标移开输入框时走的事件 -->
        <el-input
          placeholder="用户名"
          prefix-icon="el-icon-user"
          v-model="username"
          clearable
          @blur="check_username"
        ></el-input>
        <el-input
          placeholder="密码"
          prefix-icon="el-icon-key"
          v-model="password"
          clearable
          show-password
          @blur="check_password"
        ></el-input>
        <el-button type="primary" @click="login">登录</el-button>
        <div class="foot">
          <span @click="change_password" class="change">密码你都忘记了？</span>
          <span @click="go_register" class="register">立即注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
      ico: "",
      ico_url: ""
    };
  },
  methods: {
    close_logins() {
      this.$emit("close");
    },
    go_register() {
      this.$emit("go");
    },
    change_password() {
      this.$emit("change");
    },
    login() {
      if (!this.username || !this.password)
        return this.$notify({
          title: "请输入账号或密码",
          type: "error",
          duration: 1500
        });
      this.$axios({
        url: this.$settings.base_url + "/user/login/",
        method: "post",
        data: {
          username: this.username,
          password: this.password
        }
      })
        .then(response => {
          // 要将响应的用户信息和token存储到cookies中
          // 设置cookies cookies名，values，存活期

          this.ico = response.data.results.ico.replace(
            "http://127.0.0.1:8000",
            this.$settings.base_url
          );

          this.$cookies.set("token", response.data.results.token, "1d");
          this.$cookies.set("username", response.data.results.username, "1d");
          this.$cookies.set("ico", this.ico, "1d");
          // 弹出框提示后，关闭登录界面
          this.$notify({
            title: "登录成功",
            type: "success",
            duration: 1000,
            // #结束事件
            onClose: () => {
              this.$emit("success");
            }
          });
        })
        .catch(() => {
          // 异常
          this.$notify({
            title: "登入失败,请遵循格式",
            type: "error",
            duration: 1500,
            // 清除账号密码框内容
            onClose: () => {
              this.username = "";
              this.password = "";
            }
          });
        });
    },
    check_username() {
      if (this.username.length < 8 || this.username.length > 16)
        return this.$notify({
          title: "账号必须是8至16位",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.username = "";
          }
        });
      if (this.username.match(/^[A-Za-z]/) == null)
        return this.$notify({
          title: "账号必须以大小写字母为开头",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.username = "";
          }
        });
      this.$axios({
        url: this.$settings.base_url + "/user/verify_username/",
        method: "get",
        params: {
          username: this.username
        }
      })
        .then(response => {
          // 如果状态码恒等于0则报warning
          if (response.data.status === 0) {
            // 警告窗方法
            this.$notify({
              title: response.data.msg,
              type: "warning",
              // 警告框停留世间
              duration: 1000,
              onClose: () => {
                this.username = "";
              }
            });
          }
        })
        .catch(error => {
          //如果接口状态码不是200则返回以下错误
          this.$notify({
            title: error.response.data.msg,
            type: "error",
            duration: 1000,
            onClose: () => {
              this.username = "";
            }
          });
        });
    },
    check_password() {
      if (this.password.length < 8 || this.password.length > 16)
        return this.$notify({
          title: "密码必须是8至16位",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.password = "";
          }
        });
    }
  }
};
</script>

<style scoped>
.login {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  background-color: rgba(0, 0, 0, 0.3);
}

.box {
  width: 400px;
  height: 420px;
  background-color: white;
  border-radius: 10px;
  position: relative;
  top: calc(50vh - 210px);
  left: calc(50vw - 200px);
}

.el-icon-close {
  position: absolute;
  font-weight: bold;
  font-size: 20px;
  top: 10px;
  right: 10px;
  cursor: pointer;
}

.el-icon-close:hover {
  color: darkred;
}

.content {
  position: absolute;
  top: 40px;
  width: 280px;
  left: 60px;
}

.nav {
  font-size: 20px;
  height: 38px;
  border-bottom: 2px solid darkgrey;
}

.nav > span {
  margin: 0 0 0 100px;
  color: black;
  user-select: none;
  cursor: pointer;
  font: 20px Extra large;
  font-family: "Microsoft YaHei";
}

.el-input,
.el-button {
  margin-top: 40px;
  font: 13px Small;
  font-family: "Microsoft YaHei";
}

.el-button {
  width: 100%;
  font-size: 18px;
}

.foot .change {
  float: left;
  margin-top: 20px;
  color: orange;
  cursor: pointer;
  font: 16px Medium;
  font-family: "Microsoft YaHei";
}
.foot .register {
  float: right;
  margin-top: 20px;
  color: orange;
  cursor: pointer;
  font: 16px Medium;
  font-family: "Microsoft YaHei";
}
</style>